fix: terminate backends before dropping databases and close pg-cache pools in codegen#847
Merged
pyramation merged 1 commit intomainfrom Mar 17, 2026
Conversation
…pools in codegen - safeDropDb now catches 'is being accessed by other users' errors, terminates backends via pg_terminate_backend, and retries the drop - PgpmModuleSchemaSource.fetch() now releases pg-cache pool for the ephemeral database before calling teardown - generateMulti() shared PGPM source teardown now releases pg-cache pools before dropping ephemeral databases These changes fix the systemic issue where dropdb fails because deployPgpm() and getPgPool() cache connections in pg-cache that are not closed before the database is dropped.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the systemic
dropdbfailure when ephemeral databases have lingering connections:Two-layer fix:
Safety net in
DbAdmin.safeDropDb()(pgsql-client): Whendropdbfails due to active sessions, terminates backends viapg_terminate_backendand retries — only in the catch path, so the happy path is unchanged and we get log visibility into when this happens.Proper pool cleanup in codegen PGPM paths (
graphql/codegen):deployPgpm()andgetPgPool()cache connections inpg-cachethat were never released before the ephemeral database was dropped. Now explicitly callspgCache.delete(dbName)+waitForDisposals()before teardown in bothPgpmModuleSchemaSource.fetch()andgenerateMulti()shared source cleanup.Review & Testing Checklist for Human
pgCache.delete()key matchesgetPgPool()key:getPgPool()caches byconfig.database(the database name). The cleanup usesdbConfig.database/shared.ephemeralDb.config.database. Confirm these always resolve to the same key.buildSchemaSDL()→makeSchema()creates its own connection viamakePgServicethat is not in pg-cache. ThesafeDropDbterminate-and-retry is the only safety net for these. Consider whethermakeSchemashould be patched to release its pool, or if the safety net is sufficient.pgpmModulePathorpgpmWorkspacePathand verify the ephemeral database is cleanly dropped without the "is being accessed" warning.Notes
safeDropDbterminate-and-retry pattern already existed inpgpm test-packagesandpgpm killcommands — this just brings it to the coreDbAdminclass so all consumers benefit.pg_terminate_backendand the retrydropdb, but this is extremely unlikely during test/codegen cleanup.Link to Devin session: https://app.devin.ai/sessions/ca50a2babd834e40a9d5d00b379bd459
Requested by: @pyramation